package com.example.lor.eslpod;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {

    private static Document doc = null;
    private static String lessonText = null;
    private static StringBuffer lessonUrl = null;
    private static List mp3UrlList = new ArrayList();
    private static List lessonUrlList = new ArrayList();
    private static List lessonTitleList = new ArrayList();
    private ListView listView;
    private TextView lessonTextView;
    private Button buttonPlay, buttonStop, buttonUpdate;
    private StreamPlayer splayer;
    private Elements titleElements;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new MyHandler();
        splayer = new StreamPlayer();

        lessonTextView = (TextView) findViewById(R.id.lessonText);
        lessonTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
        lessonTextView.setText(Html.fromHtml("暂无数据，请更新"));

        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new ListOnItemClickListener());
//        listView.setOnScrollListener(new ListOnScrollListener());

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (splayer.mediaPlayer.isPlaying()) {
                    splayer.pause();
                    buttonPlay.setText("播放");
                } else {
                    splayer.play();
                    buttonPlay.setText("暂停");
                }
            }
        });
        buttonStop = (Button) findViewById(R.id.buttonStop);
        buttonStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                splayer.mediaPlayer.seekTo(0);
                splayer.pause();
                buttonPlay.setText("播放");
            }
        });
        buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
        buttonUpdate.setOnClickListener(new UpdateOnClickListener());

    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //只有主线程才能操作View
                    listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, lessonTitleList));
                    lessonTextView.setText("");
                    break;
                case 2:
                    lessonTextView.setText(Html.fromHtml(lessonText));
                    //splayer.playUrl(mp3Url);
                    break;
            }
            super.handleMessage(msg);
        }
    }

    class ListOnItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        doc = Jsoup.connect((String) lessonUrlList.get(position)).get();
                        lessonText = doc.select("table.podcast_table_home:nth-child(6) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > span:nth-child(1)").html();

                        Message msg = new Message();
                        msg.what = 2;
                        handler.sendMessage(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    class ListOnScrollListener implements ListView.OnScrollListener {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lessonTitleList));
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    }

    class UpdateOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //Android不能在主线程去访问网络，需要另起线程去访问
                        doc = Jsoup.connect("http://www.eslpod.com/website/index_new.html").get();
                        Elements lessonElements = doc.select("body > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2)");

                        titleElements = lessonElements.select(".podcast_title a");
                        lessonTitleList.clear();
                        for (Element title : titleElements) {
                            lessonTitleList.add(title.text());
                            lessonUrlList.add(title.absUrl("href"));
                        }

                        Elements mp3UrlElements = lessonElements.select("tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > span:nth-child(4) > span:nth-child(4) > a:nth-child(3)");
                        mp3UrlList.clear();
                        for (Element mp3UrlElement : mp3UrlElements) {
                            mp3UrlList.add(mp3UrlElement.absUrl("href"));
                        }

                        Message msg = new Message();
                        msg.what = 1;
                        handler.sendMessage(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
